/**
 * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 *
 */
package com.smarthome.core.deviceManager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShEquipmentDevCameraService;
import com.smarthome.core.deviceManager.EquipmentDevCameraService;
import com.smarthome.domain.bean.ShEquipmentDevCamera;
import com.smarthome.domain.enums.EquipmentStatusEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.rpc.HkVideoService;

@Service("equipmentDevCameraService")
public class EquipmentDevCameraServiceImpl implements EquipmentDevCameraService {
	@Resource
	private ShEquipmentDevCameraService shEquipmentDevCameraService;
	@Resource
	private HkVideoService hkvideoService;

	@Override
	public DataGrid dataGrid(ShEquipmentDevCamera devCamera, PageHelper page) {
		return shEquipmentDevCameraService.queryDataGrid(devCamera, page);
	}

	@Override
	public void saveOrUpdate(ShEquipmentDevCamera devCamera) {
		shEquipmentDevCameraService.saveOrUpdate(devCamera);
	}

	@Override
	public ShEquipmentDevCamera queryById(Long id) {
		return shEquipmentDevCameraService.selectEntryById(id);
	}

	@Override
	public int delete(Long id) {
		return shEquipmentDevCameraService.deleteByIds(id);
	}

	@Override
	public ShEquipmentDevCamera queryByCode(String code) {
		ShEquipmentDevCamera devCamera = new ShEquipmentDevCamera();
		devCamera.setCode(code);
		List<ShEquipmentDevCamera> cas = shEquipmentDevCameraService.selectEntryList(devCamera);
		if (cas != null && cas.size() > 0) {
			return cas.get(0);
		}
		return null;
	}

	@Override
	public List<ShEquipmentDevCamera> getAllList(ShEquipmentDevCamera camera) {
		return shEquipmentDevCameraService.selectEntryList(camera);
	}

	@Override
	public void parseCameraInfo() {
		// TODO Auto-generated method stub
		// 1.获取系统中所有摄像头信息
		ShEquipmentDevCamera camera = new ShEquipmentDevCamera();
		List<ShEquipmentDevCamera> cameras = getAllList(camera);
		// 2.从萤石云获取设备信息
		for (ShEquipmentDevCamera one : cameras) {
			one.setStatus(EquipmentStatusEnum.NOAVAILABLE.getCode());
			// 3.获取摄像头状态
			String content = hkvideoService.getCameraInfo(one.getCode());
			if (StringUtils.isNotBlank(content) && content.contains("\"status\":1")) {
				one.setStatus(EquipmentStatusEnum.AVAILABLE.getCode());
			}
			one.setSdkParam(content);
			// 4.入库
			shEquipmentDevCameraService.saveOrUpdate(one);
		}
	}
}
